var isIE = (navigator.appName.indexOf('Internet Explorer') != -1) ? true : false;

function update(name, param) {
	jqName = '#' + name;
	if (param != undefined) {
		var xmlurl = parent.xsltData[name].xslturl + '&' + param;
	} else {
		var xmlurl = parent.xsltData[name].xslturl;
	}
	$(jqName).transform({xml:xmlurl, xsl:parent.xsltData[name].xmlurl});
}

function sendDataToServer(func, param, dialog, update) {
	$.ajax({
		type: "POST",
		url: "../../../../bin/kernel.py?func=" + func,
		data: "name=" + param,
		success: function(msg){
			parent.update_api.update(update);
			parent.$("#" + dialog).dialog('close');
		}
	});
}

function returnCooler(id){
	$.ajax({
		type: "POST",
		url: "../../../../bin/kernel.py?func=returnCooler",
		data: "id=" + id,
		success: function(msg){
			if (msg == '') {
				alert("returned!");
			}
			update("coolerinventorydel");
		}
	});
}

function CloseOrder(qty, id, qtyordered) {
	if (qty == ""){
		qty = 0;
	}
	if ( qty > qtyordered) {
		qty = qtyordered;
	}
	$.ajax({
		type: "POST",
		url: "../../../../bin/kernel.py?func=CloseOrder",
		data: "qty=" + qty + "&id=" + id,
		success: function(msg){
			alert("Order " + id + " closed.");
			update("closeorder");
		}
	});
}

function GET() {
	var tmp = new Array();
	var tmp2 = new Array();
	var param = new Array();
	
	var get = location.search;
	
	if(get != '') {
		tmp = (get.substr(1)).split('&');
		for(var i=0; i < tmp.length; i++) {
			tmp2 = tmp[i].split('=');
			param[tmp2[0]] = tmp2[1];
		}
		return param
	}
}

function getItem(id, descr_rus){
	$.ajax({
		type: "POST",
		url: "../../../../bin/kernel.py?func=getMSPItem",
		data: "id=" + id,
		success: function(msg){
			eval('msg='+msg);
			$('#code').val(msg.code);
			$('#iunit')[0].options[msg.unit-1].selected = true;
			if (msg.integer == 0) {
				$('#integer')[0].options[0].selected = true;
			} else {
				$('#integer')[0].options[1].selected = true;
			}
			$('#safety_stock').val(msg.safety_stock);
			if (msg.planning_type == 0) {
				$('#planning_type')[0].options[0].selected = true;
			} else {
				$('#planning_type')[0].options[1].selected = true;
			}
			$('#planning_condition').val(msg.planning_condition);
			$('#description').val(msg.description);
			$('#description_rus').val(descr_rus);
			for (i = 0; i < $('#imodel')[0].length; i++) {
				$('#imodel')[0].options[i].selected = false;
			}
			for (i = 0; i < msg.model.length; i++){
				$('#imodel')[0].options[msg.model[i]-1].selected = true;
			}
		}
	});
}

function editRT(id) {
    $.ajax({
	type: "POST",
	url: "../../../../bin/kernel.py?func=getMRTItem",
	data: "id=" + id,
	success: function(msg){
	    msg = msg.replace(/u\'/g, "'");
	    eval('msg='+msg);
	    $('#autoRepairCodeInput').val(msg.code);
	    $('#autoRepairCodeDiv').html(msg.code);
	    $('#repairType').val(msg.repairType);
	    $('#repairTypeRus').val(msg.repairTypeRus);
	    $('#GUID').val(msg.SparePartsConsumption);
	    parent.dialogData.SPConsumption.getid2 = msg.SparePartsConsumption;
	    $('#imodel').val(msg.model);
	    $('#repairTypePrice').val(msg.RepairTypePrice);
	    $('#repairPartsPrice').val(msg.SparePartsPrice);
	    $('#icurrency').val(msg.currency);
	    $('#customer').val(msg.customer);
	}
    });
};

function convertDate(date) {
	if (date != "") {
		dateTMP = date.split('-');
		return dateTMP[2] + '-' + dateTMP[1] + '-' + dateTMP[0];
	} else {
		return ""
	}
}

/*function utf8_win($s){
$s= strtr ($s, array ('%20'=>' ', '\xd0\xb0'=>'а', '\xd0\x90'=>'А', 
	'\xd0\xb1'=>'б', '\xd0\x91'=>'Б', '\xd0\xb2'=>'в', '\xd0\x92'=>'В', 
	'\xd0\xb3'=>'г', '\xd0\x93'=>'Г', '\xd0\xb4'=>'д', '\xd0\x94'=>'Д', 
	'\xd0\xb5'=>'е', '\xd0\x95'=>'Е', '\xd1\x91'=>'ё', '\xd0\x81'=>'Ё', 
	'\xd0\xb6'=>'ж', '\xd0\x96'=>'Ж', '\xd0\xb7'=>'з', '\xd0\x97'=>'З', 
	'\xd0\xb8'=>'и', '\xd0\x98'=>'И', '\xd0\xb9'=>'й', '\xd0\x99'=>'Й', 
	'\xd0\xba'=>'к', '\xd0\x9a'=>'К', '\xd0\xbb'=>'л', '\xd0\x9b'=>'Л', 
	'\xd0\xbc'=>'м', '\xd0\x9c'=>'М', '\xd0\xbd'=>'н', '\xd0\x9d'=>'Н', 
	'\xd0\xbe'=>'о', '\xd0\x9e'=>'О', '\xd0\xbf'=>'п', '\xd0\x9f'=>'П', 
	'\xd1\x80'=>'р', '\xd0\xa0'=>'Р', '\xd1\x81'=>'с', '\xd0\xa1'=>'С', 
	'\xd1\x82'=>'т', '\xd0\xa2'=>'Т', '\xd1\x83'=>'у', '\xd0\xa3'=>'У', 
	'\xd1\x84'=>'ф', '\xd0\xa4'=>'Ф', '\xd1\x85'=>'х', '\xd0\xa5'=>'Х', 
	'\xd1\x86'=>'ц', '\xd0\xa6'=>'Ц', '\xd1\x87'=>'ч', '\xd0\xa7'=>'Ч', 
	'\xd1\x88'=>'ш', '\xd0\xa8'=>'Ш', '\xd1\x89'=>'щ', '\xd0\xa9'=>'Щ', 
	'\xd1\x8a'=>'ъ', '\xd0\xaa'=>'Ъ', '\xd1\x8b'=>'ы', '\xd0\xab'=>'Ы', 
	'\xd1\x8c'=>'ь', '\xd0\xac'=>'Ь', '\xd1\x8d'=>'э', '\xd0\xad'=>'Э', 
	'\xd1\x8e'=>'ю', '\xd0\xae'=>'Ю', '\xd1\x8f'=>'я', '\xd0\xaf'=>'Я'));
return $s;
} */

function editRepairItem(id) {
	$.ajax({
		type: "POST",
		url: "../../../../bin/kernel.py?func=getRepairItem",
		data: "id=" + id,
		success: function(msg){
			msg = msg.replace(/u\'/g, "'");
			eval('msg='+msg);
			loadCooler(msg['cooler'], msg);
			$('#rnumberdiv').text(msg['repairnumber']);
			$('#rnumber').val(msg['repairnumber']);
			$('#rdate').val(convertDate(msg['repairdate']));
			$('#ddate').val(convertDate(msg['dispatchdate']));
			$('#dnote').val(msg['dispatchnote']);
			$('#snote').val(msg['specialnote']);
			$('#brand').val(msg['brand']);
			$('#GUID').val(msg['otherspareparts']);
			parent.dialogData.SPConsumption.getid = msg['otherspareparts'];
			parent.dialogData.RTConsumption.getid = msg['otherspareparts'];
		}
	});
}

function loadCooler(id, msg) {
	if (id != 0) {
		$.ajax({
			type: "POST",
			url: "../../../../bin/kernel.py?func=getCoolerById",
			data: "id=" + id,
			success: function(msg){
			    
				msg = msg.replace(/u\'/g, "'");
				eval('msg='+msg);
				$('#snumber').val(0); //Fix for IE and Firefox
				$('#snumber').val(id);
				$('#inumber').val(msg['inumber']);
				$('#inumber')[0].disabled=true;
				$('#imodel').val(msg['model']);
				$('#imodel')[0].disabled=true;
				$('#destination').val(msg['destination']);
				$('#destination')[0].disabled=true;
				$('#customer').val(msg['customer']);
				$('#customer')[0].disabled=true;
				parent.dialogData.SPConsumption.getCustomer = $('#customer').val()
				parent.dialogData.RTConsumption.getCustomer = $('#customer').val()
				parent.dialogData.SPConsumption.getModel = $('#imodel').val()
				parent.dialogData.RTConsumption.getModel = $('#imodel').val()
				if (msg != undefined){
					if ($('#snumber').val() == null) {
						objSel = $('#snumber')[0]
						objSel.options[objSel.options.length] = new Option(msg['snumber'], id);
						$('#snumber').val(id);
					} else {
						if ($('#snumber').val() == 0) { //Fix for IE and Firefox
							objSel = $('#snumber')[0]
							objSel.options[objSel.options.length] = new Option(msg['snumber'], id);
							$('#snumber').val(id);
						}
					}
				}
			}
		});
	}
}

function GetXLStable(html) {
	tcount = new Date().getTime();
	//html = html.replace('&amp', '<and>');
	//html = html.replace('&', '<and>');
	html = encodeURIComponent(html);
	$.ajax({
			type: "POST",
			url: "../../../../bin/kernel.py?func=getXLSfile&_=" + tcount,
			data: "htmldata=" + html,
			success: function(msg){
				eval(msg);
			}
		});
}

function addSpaceCustomer() {
	objSel = $('#customer')[0];
	if (objSel != undefined) {
		objSel.options[objSel.options.length] = new Option('', 0);
		$('#customer').val(0);	
	} else {
		setTimeout(addSpaceCustomer, 1000);
	}				
}

function addSpaceDestination() {
	objSel = $('#destination')[0];
	if (objSel != undefined) {
		objSel.options[objSel.options.length] = new Option('', 0);
		$('#destination').val(0);
	} else {
		setTimeout(addSpaceDestination, 1000);
	}				
}

$(function() {
		$("button, input:submit, input:button").button();
	});

